﻿using SnakeGame.Lesson1;
using SnakeGame.Lesson4;
using SnakeGame.Lesson5;
using SnakeGame.Lesson6;
using System;

namespace SnakeGame.Lesson2
{
    internal class GameScene : ISceneUpdate
    {
        private readonly Map map;
        private readonly Snake snake;
        private readonly Food food;
        private bool isDrawMap = false; // 是否绘制地图
        private bool isDrawFood = false; // 是否绘制食物

        public GameScene()
        {
            map = new Map();
            snake = new Snake(20, 20); // 初始化蛇的位置
            food = new Food(snake); // 初始化食物
        }

        public void Update()
        {
            if (!isDrawMap)
            {
                map.Draw();
                isDrawMap = true; // 设置地图已绘制
            }

            if (!isDrawFood)
            {
                food.Draw(); // 绘制食物
                isDrawFood = true; // 设置食物已绘制
            }

            snake.Move(); // 移动蛇
            snake.Draw();

            // 检测蛇是否碰撞
            if (snake.IsCollision(map))
            {
                // 游戏结束
                Game.ChangeScene(E_SceneType.End);
            }
             // 检测蛇是否吃到食物
            if (snake.EatFood(food))
            {
                isDrawFood = false; // 重置食物绘制状态
            }

            // 检测键盘输入
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                switch (key.Key)
                {
                    case ConsoleKey.W:
                        snake.SetDirection(E_MoveDirection.Up);
                        break;

                    case ConsoleKey.S:
                        snake.SetDirection(E_MoveDirection.Down);
                        break;

                    case ConsoleKey.A:
                        snake.SetDirection(E_MoveDirection.Left);
                        break;

                    case ConsoleKey.D:
                        snake.SetDirection(E_MoveDirection.Right);
                        break;
                }
            }
        }
    }
}